#Region "using directives"
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.Messaging
#End Region
Namespace MapiApplication
	Public Partial Class frmCreateNew
	Inherits Form
		#Region "Globals"
		Private objMapiManager As MapiManager = Nothing
		Private options As SendMailOptions
		Private mail As Mail = New Mail()
		Private credential As Credential
		Private credentialList As List(Of String) = Nothing
		#End Region
		#Region "Events"
		Public Sub New()
		End Sub
		Public Sub New(ByVal mapiManager As MapiManager, ByRef credentialList As List(Of String))
			InitializeComponent()
			objMapiManager = mapiManager
			Me.credentialList = credentialList
		End Sub
		Public Sub New(ByVal mapiManager As MapiManager, ByVal toNameList As String, ByVal ccNameList As String, ByVal subject As String, ByVal messageBody As String)
			InitializeComponent()
			objMapiManager = mapiManager
			txtTo.Text = toNameList
			txtCc.Text = ccNameList
			txtSubject.Text = subject
			txtMessagebody.Text = messageBody
		End Sub
		Private Sub btnAttachment_Click(ByVal sender As Object, ByVal e As EventArgs)
			Browse()
		End Sub
		Private Sub toolStripAttachment_Click(ByVal sender As Object, ByVal e As EventArgs)
			Browse()
		End Sub
		Private Sub toolstripSend_Click(ByVal sender As Object, ByVal e As EventArgs)
			SendMail()
		End Sub
		Private Sub frmCreateNew_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
			If e.KeyCode = Keys.Escape Then
				Me.Close()
			End If
		End Sub
		#End Region
		#Region "Helper Methods"
		Private Sub Browse()
			Dim fd As OpenFileDialog = New OpenFileDialog()
			fd.Title = "Select File"
			fd.InitialDirectory = "c:\"
			fd.Filter = "All files (*.*)|*.*"
			fd.FilterIndex = 2
			fd.RestoreDirectory = True
            If fd.ShowDialog() = Windows.Forms.DialogResult.OK Then
                If txtAttachment.Text <> [String].Empty Then
                    txtAttachment.Text += ";" + fd.FileName
                Else
                    txtAttachment.Text = fd.FileName
                End If
            End If
		End Sub
		Private Sub SendMail()
			Try
				Dim toList As List(Of String) = New List(Of String)()
				Dim ccList As List(Of String) = New List(Of String)()
				Dim bccList As List(Of String) = New List(Of String)()
				Dim attachedFilesList As List(Of String) = New List(Of String)()
				Dim splitter As Char() = {";"C}
				Dim toNames As String()
				toNames = txtTo.Text.Split(splitter)
				For Each [to] As String In toNames
					If [to] <> [String].Empty Then
						toList.Add([to].Trim())
					End If
				Next
				Dim ccNames As String()
				ccNames = txtCc.Text.Split(splitter)
				For Each cc As String In ccNames
					If cc <> [String].Empty Then
						ccList.Add(cc.Trim())
					End If
				Next
				Dim bccNames As String()
				bccNames = txtBcc.Text.Split(splitter)
				For Each bcc As String In bccNames
					If bcc <> [String].Empty Then
						bccList.Add(bcc.Trim())
					End If
				Next
				Dim attachedFiles As String()
				attachedFiles = txtAttachment.Text.Split(splitter)
				For Each file As String In attachedFiles
					If file <> [String].Empty Then
						attachedFilesList.Add(file.Trim())
					End If
				Next
				If chkSmtp.Checked Then
					mail.Attachments = attachedFilesList
					mail.ToList = toList
					mail.CcList = ccList
					mail.BccList = bccList
					mail.Subject = txtSubject.Text
					mail.MessageBody = txtMessagebody.Text
					Me.objMapiManager.SendMailThroughSMTPServer(mail)
					Me.Close()
				Else
					objMapiManager.SendMail(toList, ccList, bccList, txtSubject.Text, txtMessagebody.Text, attachedFilesList)
					Me.Close()
				End If
			Catch mapiException As MapiManagerException
				MessageBox.Show(mapiException.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch argNullException As ArgumentNullException
				MessageBox.Show(argNullException.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch argException As ArgumentException
				MessageBox.Show(argException.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch invalidOperation As InvalidOperationException
				MessageBox.Show(invalidOperation.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch nullrefernceException As NullReferenceException
				MessageBox.Show(nullrefernceException.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		#End Region
		Private Sub chkSmtp_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
			If chkSmtp.Checked Then
				Me.toolStripCredential.Visible = True
				Me.toolStripOptions.Visible = True
			Else
				Me.toolStripCredential.Visible = False
				Me.toolStripOptions.Visible = False
			End If
		End Sub
		Private Sub toolStripCredential_Click(ByVal sender As Object, ByVal e As EventArgs)
			credential = New Credential(Me.objMapiManager, credentialList)
			credential.ShowDialog()
		End Sub
		Private Sub toolStripOptions_Click(ByVal sender As Object, ByVal e As EventArgs)
			options = New SendMailOptions(Me.mail, Me.objMapiManager)
			options.ShowDialog()
		End Sub
	End Class
End Namespace
